package com.woniu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.Result;
import com.woniu.dto.OrderDto;
import com.woniu.dto.OrderPayDto;
import com.woniu.pojo.Order;
import com.woniu.service.IOrderService;
import com.woniu.vo.OrderQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yellow Docker
 * @since 2022-11-16
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;


    @ApiOperation("通过userId查找所有订单")
    @GetMapping("/user/{userId}")
    public Result getByUserId(@PathVariable Integer userId){
        List<Order> orders = orderService.getByUserId(userId);
        return Result.ok(orders);
    }

    @ApiOperation("生成订单")
    @PostMapping
    public Result<String> createOrder(@RequestBody OrderDto orderDto){
        String orderSn = orderService.createOrder(orderDto);
        return Result.ok(orderSn);
    }

    @ApiOperation("通过orderSn查询订单")
    @GetMapping("{orderSn}")
    public Result<OrderQueryVo> findOrderByOrderSn(@PathVariable String orderSn){
        LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Order::getOrderSn, orderSn);Order order = orderService.getOne(wrapper);
        OrderQueryVo orderQueryVo = new OrderQueryVo();
        BeanUtils.copyProperties(order, orderQueryVo);
        return Result.ok(orderQueryVo);
    }

    @ApiOperation("通过id查找shopId")
    @GetMapping("/shop/{id}")
    public Result getShopId(@PathVariable Integer id){
        Integer shopId = orderService.getShopId(id);
        return Result.ok(shopId);
    }




    @ApiOperation("修改订单状态")
    @PutMapping
    public Result<String> updateOrderStatus(@RequestBody OrderPayDto orderPayDto){
         int result = orderService.updateOrderStatus(orderPayDto);
         return result == 1 ? Result.ok("success") : Result.ok("fail");
    }

    @ApiOperation("修改订单状态为已完成")
    @PutMapping("/updateStatus/{orderSn}")
    public Result<String> updateStatus(@PathVariable String orderSn){
        int result = orderService.updateStatus(orderSn);
        return result == 1 ? Result.ok("success") : Result.ok("fail");
    }

    @ApiOperation("修改订单状态为已付款")
    @PutMapping("/updateStatusPro/{orderSn}")
    public Result<String> updateStatusPro(@PathVariable String orderSn){
        int result = orderService.updateStatusPro(orderSn);
        return result == 1 ? Result.ok("success") : Result.ok("fail");
    }
}

